﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP.Reports
{
    public partial class AdsReport : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Items.Add("Date");
                DropDownList1.Items.Add("Link");
                DropDownList1.Items.Add("Duration");
                DropDownList1.Items.Add("Status");
                DropDownList1.Items.Add("Price");

                SqlCommand cmdReport;
                SqlDataReader dtrReport;
                conn.Open();

                cmdReport = new SqlCommand("SELECT a_id, a_link, a_status, a_duration, CONVERT(varchar, a_date, 103) AS adate, a_price FROM Advertisements ORDER BY a_date", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList.DataSource = dtrReport;
                ItemList.DataBind();
                dtrReport.Close();

                cmdReport = new SqlCommand("SELECT SUM(a_price) FROM Advertisements", conn);
                double total = Convert.ToDouble(cmdReport.ExecuteScalar());
                lblTearn.Text = total.ToString();
                conn.Close();

                MultiView1.ActiveViewIndex = 0;
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.open('"+ msg +"', '_blank');</script>";
            Page.Controls.Add(lbl);
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlCommand cmdReport;
            SqlDataReader dtrReport;
            conn.Open();

            if (RadioButtonList1.SelectedValue == "Daily")
            {
                TextBox1.Text = "";
                TextBox3.Text = "";
                CheckBox1.Checked = false;
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("Date");
                DropDownList1.Items.Add("Link");
                DropDownList1.Items.Add("Duration");
                DropDownList1.Items.Add("Status");
                DropDownList1.Items.Add("Price");

                cmdReport = new SqlCommand("SELECT a_id, a_link, a_status, a_duration, CONVERT(varchar, a_date, 103) AS adate, a_price FROM Advertisements ORDER BY a_date", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList.DataSource = dtrReport;
                ItemList.DataBind();
                dtrReport.Close();

                cmdReport = new SqlCommand("SELECT SUM(a_price) FROM Advertisements", conn);
                double total = Convert.ToDouble(cmdReport.ExecuteScalar());
                lblTearn.Text = total.ToString();

                MultiView1.ActiveViewIndex = 0;
                Panel1.Visible = true;
                Panel2.Visible = false;
            } 
            else if (RadioButtonList1.SelectedValue == "Monthly")
            {
                cmdReport = new SqlCommand("SELECT DISTINCT(DATEPART(year, a_date)) AS year FROM Advertisements", conn);
                dtrReport = cmdReport.ExecuteReader();
                ddlMonth.DataSource = dtrReport;
                ddlMonth.DataTextField = "year";
                ddlMonth.DataBind();
                dtrReport.Close();
                ddlMonth.Items.Insert(0, new ListItem("-------", String.Empty));
                ddlMonth.SelectedIndex = 0;

                TextBox1.Text = "";
                TextBox3.Text = "";
                CheckBox1.Checked = false;
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("Month");
                DropDownList1.Items.Add("Advertisements");
                DropDownList1.Items.Add("Total Earning");

                cmdReport = new SqlCommand("SELECT DATEPART(year, a_date) AS tyear, DATENAME(month, a_date) AS tmonth, COUNT(a_id) AS tads, SUM(a_price) AS tearn FROM Advertisements GROUP BY DATEPART(year, a_date), DATENAME(month, a_date)", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList2.DataSource = dtrReport;
                ItemList2.DataBind();
                dtrReport.Close();

                cmdReport = new SqlCommand("SELECT COUNT(a_id) FROM Advertisements", conn);
                int tads = Convert.ToInt32(cmdReport.ExecuteScalar());
                lblTads2.Text = tads.ToString();

                cmdReport = new SqlCommand("SELECT SUM(a_price) FROM Advertisements", conn);
                double total = Convert.ToDouble(cmdReport.ExecuteScalar());
                lblTotal2.Text = total.ToString();

                MultiView1.ActiveViewIndex = 1;
                Panel1.Visible = false;
                Panel2.Visible = true;
            }
            else if (RadioButtonList1.SelectedValue == "Yearly")
            {
                CheckBox1.Checked = false;
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("Year");
                DropDownList1.Items.Add("Advertisements");
                DropDownList1.Items.Add("Total Earning");

                cmdReport = new SqlCommand("SELECT DATEPART(year, a_date) AS tyear, COUNT(a_id) AS tads, SUM(a_price) AS tearn FROM Advertisements GROUP BY DATEPART(year, a_date)", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList3.DataSource = dtrReport;
                ItemList3.DataBind();
                dtrReport.Close();

                cmdReport = new SqlCommand("SELECT COUNT(a_id) FROM Advertisements", conn);
                int tads = Convert.ToInt32(cmdReport.ExecuteScalar());
                lblTads3.Text = tads.ToString();

                cmdReport = new SqlCommand("SELECT SUM(a_price) FROM Advertisements", conn); 
                double total = Convert.ToDouble(cmdReport.ExecuteScalar());
                lblTotal3.Text = total.ToString();

                MultiView1.ActiveViewIndex = 2;
                Panel1.Visible = false;
                Panel2.Visible = false;
            }
            conn.Close();
        }

        protected void btnSort_Click(object sender, EventArgs e)
        {
            SqlCommand cmdReport, cmdReport2 ;
            SqlDataReader dtrReport;
            conn.Open();

            String sort = "", sc = "";

            if (CheckBox1.Checked)
                sc = "DESC";

            if (RadioButtonList1.SelectedValue == "Daily")
            {
                if (DropDownList1.SelectedValue == "Date")
                    sort = "a_date";
                else if (DropDownList1.SelectedValue == "Link")
                    sort = "a_link";
                else if (DropDownList1.SelectedValue == "Duration")
                    sort = "a_duration";
                else if (DropDownList1.SelectedValue == "Status")
                    sort = "a_status";
                else if (DropDownList1.SelectedValue == "Price")
                    sort = "a_price";

                if (TextBox1.Text != "" && TextBox3.Text != "")
                {
                    cmdReport = new SqlCommand("SELECT a_id, a_link, a_status, a_duration, CONVERT(varchar, a_date, 103) AS adate, a_price FROM Advertisements WHERE CONVERT(DATE, a_date, 103) BETWEEN CONVERT(DATE, @fdate, 103) AND CONVERT(DATE, @tdate, 103) ORDER BY " + sort + " " + sc, conn);
                    cmdReport.Parameters.AddWithValue("@fdate", TextBox1.Text);
                    cmdReport.Parameters.AddWithValue("@tdate", TextBox3.Text);

                    cmdReport2 = new SqlCommand("SELECT SUM(a_price) FROM Advertisements WHERE CONVERT(DATE, a_date, 103) BETWEEN CONVERT(DATE, @fdate, 103) AND CONVERT(DATE, @tdate, 103)", conn);
                    cmdReport2.Parameters.AddWithValue("@fdate", TextBox1.Text);
                    cmdReport2.Parameters.AddWithValue("@tdate", TextBox3.Text);
                }
                else if (TextBox1.Text != "" && TextBox3.Text == "")
                {
                    cmdReport = new SqlCommand("SELECT a_id, a_link, a_status, a_duration, CONVERT(varchar, a_date, 103) AS adate, a_price FROM Advertisements WHERE CONVERT(DATE, a_date, 103) >= CONVERT(DATE, @fdate, 103) ORDER BY " + sort + " " + sc, conn);
                    cmdReport.Parameters.AddWithValue("@fdate", TextBox1.Text);

                    cmdReport2 = new SqlCommand("SELECT SUM(a_price) FROM Advertisements WHERE (CONVERT(VARCHAR, a_date, 103) >= CONVERT(DATE, @fdate, 103))", conn);
                    cmdReport2.Parameters.AddWithValue("@fdate", TextBox1.Text);
                }
                else if (TextBox1.Text == "" && TextBox3.Text != "")
                {
                    cmdReport = new SqlCommand("SELECT a_id, a_link, a_status, a_duration, CONVERT(varchar, a_date, 103) AS adate, a_price FROM Advertisements WHERE CONVERT(DATE, a_date, 103) <= CONVERT(DATE, @tdate, 103) ORDER BY " + sort + " " + sc, conn);
                    cmdReport.Parameters.AddWithValue("@tdate", TextBox3.Text);

                    cmdReport2 = new SqlCommand("SELECT SUM(a_price) FROM Advertisements WHERE (CONVERT(DATE, a_date, 103) <= CONVERT(DATE, @tdate, 103))", conn);
                    cmdReport2.Parameters.AddWithValue("@tdate", TextBox3.Text);
                }
                else
                {
                    cmdReport = new SqlCommand("SELECT a_id, a_link, a_status, a_duration, CONVERT(varchar, a_date, 103) AS adate, a_price FROM Advertisements ORDER BY " + sort + " " + sc, conn);

                    cmdReport2 = new SqlCommand("SELECT SUM(a_price) FROM Advertisements", conn);
                }
                dtrReport = cmdReport.ExecuteReader();

                ItemList.DataSource = dtrReport;
                ItemList.DataBind();
                dtrReport.Close();

                double total = Convert.ToDouble(cmdReport.ExecuteScalar());
                lblTearn.Text = total.ToString();

                MultiView1.ActiveViewIndex = 0;
            }
            else if (RadioButtonList1.SelectedValue == "Monthly")
            {
                if (DropDownList1.SelectedValue == "Month")
                    sort = "DATEPART(year, a_date), DATEPART(month, a_date)";
                else if (DropDownList1.SelectedValue == "Advertisements")
                    sort = "tads";
                else if (DropDownList1.SelectedValue == "Total Earning")
                    sort = "tearn";

                if (ddlMonth.SelectedValue == "")
                    cmdReport = new SqlCommand("SELECT DATEPART(year, a_date) AS tyear, DATEPART(month, a_date) AS ttmonth, DATENAME(month, a_date) AS tmonth, COUNT(a_id) AS tads, SUM(a_price) AS tearn FROM Advertisements GROUP BY DATEPART(year, a_date), DATEPART(month, a_date), DATENAME(month, a_date) ORDER BY " + sort + " " + sc, conn);
                else
                {
                    cmdReport = new SqlCommand("SELECT DATEPART(year, a_date) AS tyear, DATEPART(month, a_date) AS ttmonth, DATENAME(month, a_date) AS tmonth, COUNT(a_id) AS tads, SUM(a_price) AS tearn FROM Advertisements WHERE DATEPART(year, a_date)=@tdate GROUP BY DATEPART(year, a_date), DATEPART(month, a_date), DATENAME(month, a_date) ORDER BY " + sort + " " + sc, conn);
                    cmdReport.Parameters.AddWithValue("@tdate", ddlMonth.SelectedValue);
                }
                dtrReport = cmdReport.ExecuteReader();

                ItemList2.DataSource = dtrReport;
                ItemList2.DataBind();
                dtrReport.Close();

                if (ddlMonth.SelectedValue == "")
                {
                    cmdReport2 = new SqlCommand("SELECT COUNT(a_id) FROM Advertisements", conn);

                    cmdReport = new SqlCommand("SELECT SUM(a_price) FROM Advertisements", conn);
                }
                else
                {
                    cmdReport2 = new SqlCommand("SELECT COUNT(a_id) FROM Advertisements WHERE DATEPART(year, a_date)=@tdate", conn);
                    cmdReport.Parameters.AddWithValue("@tdate", ddlMonth.SelectedValue);

                    cmdReport = new SqlCommand("SELECT SUM(a_price) FROM Advertisements WHERE DATEPART(year, a_date)=@tdate", conn);
                    cmdReport.Parameters.AddWithValue("@tdate", ddlMonth.SelectedValue);
                }
                int tads = Convert.ToInt32(cmdReport2.ExecuteScalar());
                lblTads2.Text = tads.ToString();

                double total = Convert.ToDouble(cmdReport.ExecuteScalar());
                lblTotal2.Text = total.ToString();
                MultiView1.ActiveViewIndex = 1;
            }
            else if (RadioButtonList1.SelectedValue == "Yearly")
            {
                if (DropDownList1.SelectedValue == "Year")
                    sort = "DATEPART(year, a_date)";
                else if (DropDownList1.SelectedValue == "Advertisements")
                    sort = "tads";
                else if (DropDownList1.SelectedValue == "Total Earning")
                    sort = "tearn";

                cmdReport = new SqlCommand("SELECT DATEPART(year, a_date) AS tyear, COUNT(a_id) AS tads, SUM(a_price) AS tearn FROM Advertisements GROUP BY DATEPART(year, a_date) ORDER BY " + sort + " " + sc, conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList3.DataSource = dtrReport;
                ItemList3.DataBind();
                dtrReport.Close();

                cmdReport2 = new SqlCommand("SELECT COUNT(a_id) FROM Advertisements", conn);
                int tads = Convert.ToInt32(cmdReport2.ExecuteScalar());
                lblTads3.Text = tads.ToString();

                cmdReport = new SqlCommand("SELECT SUM(a_price) FROM Advertisements", conn);
                double total = Convert.ToDouble(cmdReport.ExecuteScalar());
                lblTotal3.Text = total.ToString();
                MultiView1.ActiveViewIndex = 2;
            }

            conn.Close();
        }
    }
}